/*
 * Project Name sprint-boot-tools
 * File Name SubmitAction
 * Package Name com.huxiaosu.tools.cmpp.service.action
 * Create Time 2024/5/6
 * Create by name：liujie
 */
package com.huxiaosu.tools.cmpp.action;

import com.huxiaosu.tools.cmpp.enums.CommandId;
import com.huxiaosu.tools.cmpp.enums.ResponseStatus;
import com.huxiaosu.tools.cmpp.model.MessageHeader;
import com.huxiaosu.tools.cmpp.action.AbstractMessageAction;
import com.huxiaosu.tools.cmpp.model.action.SubmitMsg;
import com.huxiaosu.tools.cmpp.model.action.SubmitRespMsg;
import com.huxiaosu.tools.cmpp.utils.Length;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.springframework.stereotype.Component;

import static com.huxiaosu.tools.cmpp.utils.Length.Submit.*;

/**
 * SP 向 ISMG 提交短信（CMPP_SUBMIT）操作
 *
 * @author liujie
 * @date 2024/5/6 20:48
 */
@Slf4j
@Component
public class SubmitAction extends AbstractMessageAction {

    @Override
    public CommandId getMessageCommandId() {
        return CommandId.CMPP_SUBMIT;
    }

    @Override
    public String getMessageCommandName() {
        return CommandId.CMPP_SUBMIT.name();
    }

    @SneakyThrows(value = Throwable.class)
    @Override
    public SubmitMsg readMessage(IoBuffer ioBuffer) {
        SubmitMsg submit = new SubmitMsg();

        //12+8+1+1+1+1+10+1+32+1+1+1+1+6+2+6+17+17+21+1+32+1+1+msg.getBytes("GBK").length+20
        submit.setMsgId(ioBuffer.getLong());
        submit.setPkTotal(ioBuffer.get());
        submit.setPkNumber(ioBuffer.get());
        submit.setRegisteredDelivery(ioBuffer.get());
        submit.setMsgLevel(ioBuffer.get());
        byte[] serviceId = new byte[SERVICE_ID];
        ioBuffer.get(serviceId);
        submit.setServiceId(new String(serviceId));
        submit.setFeeUserType(ioBuffer.get());
        byte[] feeTerminalId = new byte[FEE_TERMINAL_ID];
        ioBuffer.get(feeTerminalId);
        submit.setFeeTerminalId(new String(feeTerminalId));
        submit.setFeeTerminalType(ioBuffer.get());
        submit.setTpPId(ioBuffer.get());
        submit.setTpUdhi(ioBuffer.get());
        submit.setMsgFmt(ioBuffer.get());
        byte[] msgRsc = new byte[MSG_SRC];
        ioBuffer.get(msgRsc);
        submit.setMsgSrc(new String(msgRsc));
        byte[] feeType = new byte[FEE_TYPE];
        ioBuffer.get(feeType);
        submit.setFeeType(new String(feeType));
        byte[] feeCode = new byte[FEE_CODE];
        ioBuffer.get(feeCode);
        submit.setFeeCode(new String(feeCode));
        byte[] valIdTime = new byte[VALID_TIME];
        ioBuffer.get(valIdTime);
        submit.setValIdTime(new String(valIdTime));
        byte[] atTime = new byte[AT_TIME];
        ioBuffer.get(atTime);
        submit.setAtTime(new String(atTime));
        byte[] srcId = new byte[SRC_ID];
        ioBuffer.get(srcId);
        submit.setSrcId(new String(srcId));
        byte destUsrTl = ioBuffer.get();
        submit.setDestUsrTl(destUsrTl);
        int msisdnLength = (int) destUsrTl * DEST_TERMINAL_ID;
        byte[] destTerminalId = new byte[msisdnLength];
        ioBuffer.get(destTerminalId);
        submit.setDestTerminalId(new String(destTerminalId));
        submit.setDestTerminalType(ioBuffer.get());
        byte messageLength = ioBuffer.get();
        submit.setMsgLength(messageLength);

        byte[] msgContent = new byte[messageLength];
        ioBuffer.get(msgContent);
        submit.setMsgContent(new String(msgContent,"GBK"));
        byte[] linkId = new byte[LINK_ID];
        ioBuffer.get(linkId);
        submit.setLinkID(new String(linkId));

        return submit;
    }

    @Override
    public void execute() {

        SubmitMsg submitMsg = (SubmitMsg) message.getMessageHeader();
        log.info("<接收 submit 消息 {}>",submitMsg.toString());
        SubmitRespMsg resp = new SubmitRespMsg();
        resp.setCommandId(CommandId.CMPP_SUBMIT_RESP.getCommand());
        resp.setSequenceId(message.getSequenceId());
        resp.setTotalLength(CommandId.CMPP_SUBMIT_RESP.getMsgLength());
        resp.setMsgId(submitMsg.getMsgId());
        resp.setResult(ResponseStatus.SUCCESS.getStatus());
        session.write(resp.toByte());
    }

}